<template>
  <div class="registerDetail">
    <van-nav-bar :title="$route.meta.title" left-text="返回" left-arrow :fixed="true" :placeholder="true" @click-left="$router.go(-1)" />
    <div>
      <van-form ref="form" @submit="saveFormData">
        <van-field readonly v-model="dataSource.billNumber" name="单据编号" label="单据编号" input-align="right" />
        <van-field readonly v-model="dataSource.vinCode" name="车架号" label="车架号" input-align="right" />
        <van-field readonly v-model="dataSource.brandName" name="品牌" label="品牌" input-align="right" />
        <van-field readonly v-model="dataSource.showOrgName" name="所属展厅" label="所属展厅" input-align="right" />
        <van-field readonly v-model="dataSource.procurementCost" name="采购成本" label="采购成本" input-align="right" />
        <van-field readonly v-model="dataSource.salePrice" name="销售价格" label="销售价格" input-align="right" />

        <br />
        <van-field v-model="dataSource.costOther" required type="number" :formatter="formatter" :rules="[{ required: true }]" name="其它整备费用" label="其它整备费用" input-align="right" />
        <van-field v-model="dataSource.costInspection" required type="number" :formatter="formatter" :rules="[{ required: true }]" name="检测费" label="检测费" input-align="right" />
        <br />
        <van-field v-model="dataSource.otherCommission" required :rules="[{ required: true }]" :formatter="formatter" type="number" name="佣金" label="佣金" input-align="right" />
        <van-field v-model="dataSource.otherTracel" required :rules="[{ required: true }]" :formatter="formatter" type="number" name="差旅费" label="差旅费" input-align="right" />
        <van-field v-model="dataSource.otherTransfer" required :rules="[{ required: true }]" name="过户费" type="number" :formatter="formatter" label="过户费" input-align="right" />
        <van-field v-model="dataSource.otherOil" required :rules="[{ required: true }]" type="number" name="加油费" :formatter="formatter" label="加油费" input-align="right" />
        <van-field v-model="dataSource.otherBoardCar" required :rules="[{ required: true }]" type="number" name="板车费" :formatter="formatter" label="板车费" input-align="right" />

        <br />

        <van-field required v-model="dataSource.proviceName" name="信息提供人" label="信息提供人" input-align="right" placeholder="请选择" right-icon="arrow" @click="handleSelect('proviceId', 'proviceName')" readonly :rules="[{ required: true }]" />
        <van-field required v-model="dataSource.assessName" name="评估人员" label="评估人员" input-align="right" placeholder="请选择" right-icon="arrow" @click="handleSelect('assessID', 'assessName')" readonly :rules="[{ required: true }]" />
        <!-- 下拉选择弹窗 -->
        <van-popup v-model="showSelectPicker" position="bottom">
          <van-field name="模糊查询" v-if="likeWhoField == 'proviceId'" input-align="left" v-model="likeWho" label="模糊查询" placeholder="请输入模糊查询关键词" />
          <van-picker show-toolbar :columns="columns" @confirm="handleSelectConfirm" @cancel="showSelectPicker = false" value-key="name" />
        </van-popup>
        <!-- <div class="footerBtn">
          <div class="submitBtn flex">
            <div class="btnbox editBtn" @click="saveFormData()">提交</div>
          </div>
        </div>-->
        <div class="footerBtn">
          <div class="submitBtnbox">
            <div class="saveBtn" @click="saveFormData(1)">保存</div>
            <div class="submitBtn" @click="saveFormData(2)">提交</div>
          </div>
        </div>
      </van-form>
    </div>
  </div>
</template>

<script>
import {
  Search,
  Empty,
  Field,
  Form,
  Uploader,
  DatetimePicker,
  Tab,
  Tabs,
  ImagePreview,
  ShareSheet,
  Dialog
} from "vant";
import { getCookies } from "@/utils/util.js";
import commonApi from "@/api/commonApi";
import sellManageApi from "@/api/sellManageApi";

export default {
  name: "taskCenterDetail",
  components: {
    Search,
    Empty,
    Field,
    Form,
    Uploader,
    DatetimePicker,
    Tab,
    Tabs,
    ImagePreview,
    ShareSheet,
    Dialog
  },
  data() {
    return {
      showTimerPicker: false,
      dataSource: {
        billId: "", //单据Id
        billNumber: "", //出库单编号
        vehicleSaleDate: "", //车辆销售日期
        billStatus: "", //收款状态
        vinCode: "", //车架号
        contNo: "", //采购合同
        saleContNo: "", //销售合同
        procurementCost: "", //采购成本
        salePrice: "", //车辆售价
        entryId: "", //分录Id
        proviceName: "", //信息提供人
        proviceId: "", //信息提供人
        assessName: "", //评估人
        assessID: "", //评估人
        capInterest: "", //资金利息
        costTotal: "", //整车费用合计
        costSheetSparay: "", //版喷整备
        costEMRepair: "", //机电修复
        costOther: "", //其他整备费用
        costInspection: "", //检测费
        costCerExtension: "", //认证延保
        costClean: "", //美容清洗
        costPrepareAmt: "", //整备工单费用
        otherTotal: "", //其他合计
        otherCommission: "", //佣金
        otherTax: "", //税费
        otherTracel: "", //差旅费
        otherTransfer: "", //过户费
        otherOil: "", //加油费
        otherBoardCar: "", //板车费
        brandId: "", //品牌ID
        brandName: "", //品牌名称
        shwoOrgName: "", //展厅名字
        showOrgId: "" //展厅ID
      },

      uploader: [],
      columns: [],
      showSelectPicker: false,
      disabled: false,
      detailId: "",
      tabActive: 0,
      recordList: [
        // {
        //   id: 1,
        //   name: '闽D66666定期保养招揽',
        //   status: 2,
        //   insure: 2,
        //   time: '2023-06-25 12:29:30',
        //   address: '福建省厦门市思明区',
        //   vin: 'LKHHAHHHAHHHHAYYYYA',
        //   tel: '13333333333',
        //   userName: '张某某',
        //   servieName: '高某某',
        //   carType: ' CT5 2,0T',
        //   mileage: '9999',
        // },
        // {
        //   id: 2,
        //   name: '闽D66666',
        //   status: 2,
        //   insure: 2,
        //   time: '2023-06-25 12:29:30',
        //   address: '福建省厦门市思明区',
        //   vin: 'LKHHAHHHAHHHHAYYYYA',
        //   tel: '16666666666',
        //   userName: '张某某',
        //   servieName: '高某某',
        //   carType: ' CT5 2,0T',
        //   mileage: '9999',
        // },
      ],
      serviceList: [], // 人员列表
      field: "",
      fieldName: "",
      userId: getCookies("userId"), //用户ID，必填，String
      likeWhoField: "",
      likeWho: "",
      useCarsProviderList: []
    };
  },

  mounted() {
    if (this.$route.query) {
      this.detailId = this.$route.query.billId;
      this.getDetail(this.detailId);
    }
  },
  watch: {
    likeWho: function(nval, oval) {
      if (nval) {
        let newArr = this.useCarsProviderList.filter(item => {
          console.log(item.name);
          if (item.name) {
            return item.name.indexOf(nval) > -1;
          }
          return null;
        });
        console.log(newArr);
        this.columns = newArr;
      } else {
        this.columns = this.useCarsProviderList;
      }
    }
  },
  methods: {
    formatter(value) {
      return value.replace(/[^\d\.]/, "").replace(/^0(?=[0-9])/, "");
    },
    saveFormData(type) {
      this.$refs.form
        .validate()
        .then(res => {
          Dialog.confirm({
            title: "提示",
            message: `确定${type == 1 ? "保存" : "提交"}？`
          })
            .then(() => {
              let formData = {
                billId: "", //单据Id，必填，String
                entryId: "", //单据分录Id，必填，String
                costOther: "", //其他整备费用，必填，BigDecimal
                costInspection: "", //检测费，必填，BigDecimal
                otherCommission: "", //佣金，必填，BigDecimal
                otherTracel: "", //差旅费，必填，BigDecimal
                otherTransfer: "", //过户费，必填，BigDecimal
                otherOil: "", //加油费，必填，BigDecimal
                otherBoardCar: "", //板车费，必填，BigDecimal
                proviceId: "", //信息提供人ID，必填，String
                assessID: "" //评估人ID，必填，String
              };
              for (const key in formData) {
                if (formData.hasOwnProperty.call(formData, key)) {
                  formData[key] = this.dataSource[key];
                }
              }
              formData.method = "updateUsedCarAttached"; //固定值
              formData.saveOrSubmit = type == 1 ? "save" : "submit"; //固定值

              sellManageApi.secondHandCarFacade(formData).then(res => {
                console.log(res);
                if (res.code == 1 && !res.msg) {
                  this.$toast.fail("更新成功");
                  this.$router.go(-1);
                } else {
                  this.$toast(res.msg);
                }
              });
            })
            .catch(() => {
              console.log("取消");
            });
        })
        .catch(res => {
          console.log(res);
          this.loading = false;
          let msg = "请完善信息," + res.map(res => res.name);
          this.$toast.fail(msg);
        });
    },
    // 选择下拉
    handleSelect(field, fieldName) {
      this.showSelectPicker = true;
      if (field == "proviceId") {
        this.columns = this.useCarsProviderList;
      }
      if (field == "assessID") {
        this.columns = this.serviceList;
      }
      this.field = field;
      this.fieldName = fieldName;
      this.likeWhoField = field;
    },
    // 选择下拉确认
    handleSelectConfirm(value, index) {
      this.dataSource[this.field] = value.billId ? value.billId : value.value;
      this.dataSource[this.fieldName] = value.name;
      console.log(this.dataSource);
      this.showSelectPicker = false;
      this.likeWho = "";
    },
    // 获取评估人员
    getEmpPerson() {
      sellManageApi
        .secondHandCarFacade({
          method: "getSecondHandPerson",
          personType: "useCars",
          fixCenterId: this.dataSource.showOrgId
        })
        .then(res => {
          this.serviceList = res.data;
        });
    },
    // 获取信息提供人
    getUseCarsProvider() {
      sellManageApi
        .secondHandCarFacade({
          method: "getSecondHandPerson",
          personType: "useCarsProvider",
          fixCenterId: this.dataSource.showOrgId
        })
        .then(res => {
          this.useCarsProviderList = res.data;
        });
    },
    getDetail(billId) {
      sellManageApi
        .secondHandCarFacade({
          billId,
          method: "getUsedCarAttachedById"
        })
        .then(res => {
          if (res.code == 1) {
            this.dataSource = res.data;
            this.getEmpPerson();
            this.getUseCarsProvider();
          }
        });
    }
  }
};
</script>

<style lang="less" scoped>
body {
  background: #f3f3f3;
}
.flex {
  display: flex;
  align-items: center;
}
.registerDetail {
  padding-bottom: 1.5rem;
  .top_detail {
    display: flex;
    align-items: center;
    // width: 100%;
    background: #fff;
    padding: 10px;
    margin: 10px;
    .top_detail_lt {
      display: inline-block;
      text-align: center;
      width: 50%;
      border-right: 1px solid #f0f0f0;
    }
    .top_detail_rt {
      width: 50%;
      display: inline-block;
      text-align: center;
    }
    .top_name {
      font-weight: bold;
      font-size: 13px;
      margin-bottom: 5px;
    }
    .top_status {
      color: red;
      font-size: 12px;
      margin-bottom: 5px;
    }
    .top_txt {
      font-size: 12px;
      color: #333;
      margin-bottom: 5px;
      .icon-tel {
        background: #35bc04;
        color: #fff;
        border-radius: 50%;
        padding: 5px;
      }
    }
  }
}

// 跟进信息
.followInfo {
  padding: 10px;
  background: #fff;

  .foll_title {
    box-sizing: border-box;
    color: #646566;
    text-align: left;
    font-size: 15px;
    padding: 5px 10px 5px 5px;
    margin-bottom: 10px;
  }
  .followInfo_item {
    margin-bottom: 10px;
    position: relative;
    .followInfo_lt {
      width: 20%;
      .followInfo_time {
        font-size: 14px;
        color: #666;
        position: absolute;
        top: 5px;
      }
      &::before {
        content: "";
        height: 130px;
        width: 1px;
        background: #ccc;
        position: absolute;
        top: 0px;
        left: 20px;
      }
      .icon-circle {
        color: #ccc;
        z-index: 2;
        position: absolute;
        top: 0px;
        left: 0.22rem;
        background: #fff;
      }
    }
    .followInfo_rt {
      background: #f1f1f1;
      width: 80%;
      padding: 15px 0 15px 15px;
      border-radius: 4px;
      .followInfo_tt {
        display: flex;
        align-items: center;
        border: 1px solid #02c081;
        font-size: 12px;
        border-radius: 3px;
        margin-bottom: 5px;
        width: 200px;

        .followInfo_status {
          background: #02c081;
          color: #fff;
          padding: 5px;
        }
        .followInfo_time {
          padding: 5px;
          margin-left: 5px;
        }
      }
      .followInfo_num {
        font-size: 14px;
        color: #333;
        margin-bottom: 5px;
      }
      .followInfo_data {
        font-size: 13px;
        color: #666;
        margin-bottom: 5px;
        .followInfo_img {
          width: 40px;
          height: 40px;
          margin-right: 5px;
        }
      }
    }
  }
}
.morebox {
  position: fixed;
  bottom: 0;
  left: 0;
  z-index: 99;
  width: 100%;
  background: #fff;
  .moreBtn {
    background: #409eff;
    color: #fff;
    width: 75%;
    margin: auto;
    text-align: center;
    padding: 12px;
    border-radius: 5px;
    margin-top: 15px;
    font-size: 15px;
    margin-bottom: 25px;
  }
}
.tableData {
  width: 100%;
  margin: auto;
  border-collapse: collapse;
  text-align: center;
  table {
    width: 100%;
    font-size: 12px;
  }
  .tab_item {
    background: #f1f1f1;
  }
  tr {
    th {
      font-size: 12px;
      border: 1px solid #ccc;
      padding: 10px 0;
      background: #409eff;
      color: #fff;
    }
    td {
      padding: 5px 0;
      border: 1px solid #ccc;
      font-size: 12px;
    }
  }
}
.footerBtn {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  background: #fff;
  padding-bottom: 20px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);

  .submitBtnbox {
    display: flex;
    width: 80%;
    margin: auto;
    margin-top: 15px;
    justify-content: space-between;
    overflow: hidden;
    box-sizing: content-box;
    .saveBtn {
      background: #ccc;
      color: #fff;
      width: 45%;
      margin: auto;
      text-align: center;
      padding: 12px;
      border-radius: 5px;
      font-size: 15px;
      margin-right: 10px;
    }
    .submitBtn {
      background: #409eff;
      color: #fff;
      width: 45%;
      margin: auto;
      text-align: center;
      padding: 12px;
      border-radius: 5px;
      font-size: 15px;
    }
  }
}
</style>
